﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using ZP.Common;
using ZP.DTO.Common;

namespace ZCSBZPJ.Filter
{
    public class AuthorityFilter : ActionFilterAttribute
    {
        public int AuthorityID
        {
            get;
            set;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 判断session是否丢失
            if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session[ZPCommon.SESSION_Login] == null)
            {
                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.RequestContext.HttpContext.Response.AppendHeader("sessionstatus", "timeout");
                    ContentResult Content = new ContentResult();
                    Content.Content = string.Format("alert('请先登录！');top.location.href='/login/index?returnUrl={0}';", filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath);
                    filterContext.Result = Content;
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                    {
                        controller = "Login",
                        action = "Index",
                        area = ""
                    }));
                }
                return;
            }

            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
            {
                base.OnActionExecuting(filterContext);
                return;
            }

            // 判断是否拥有权限
            /*UserLoginDTO lng = (UserLoginDTO)filterContext.HttpContext.Session[ZPCommon.SESSION_Login];
            var pp = lng.CurrentAuthorityList.Where(p => p.id == AuthorityID).FirstOrDefault();
            if (AuthorityID == 0 || (pp != null))
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Common",
                    action = "NoAuthorityInfo",
                    area = ""
                }));
            }*/
        }
    }
}